package com.paper.tiger.datasource.aop;

import com.paper.tiger.datasource.common.DataSource;
import org.aopalliance.aop.Advice;
import org.springframework.aop.Pointcut;
import org.springframework.aop.support.AbstractPointcutAdvisor;
import org.springframework.aop.support.ComposablePointcut;
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;

/**
 * [简要描述]: 数据源切换的advisor，执行在那些地方创建切面
 * [详细描述]:
 *Advisor
 * @author ZouHaiBo
 * @version 1.0, 2021/5/21 15:09
 * @since JDK 1.8
 */
public class DataSourceAnnotionAdvisor extends AbstractPointcutAdvisor
{

    private Advice advice;

    private Pointcut pointcut;


    public DataSourceAnnotionAdvisor(DataSourceAnnotionMethodIntercept advice)
    {
        this.advice = advice;

        // 类或者是方法上面有DataSource注解，就把他们加入到advisor
        Pointcut pointcut1 = new AnnotationMatchingPointcut(DataSource.class, true);
        Pointcut pointcut = AnnotationMatchingPointcut
                .forMethodAnnotation(DataSource.class);

       this.pointcut = new ComposablePointcut(pointcut).union(pointcut1);

    }

    @Override
    public Pointcut getPointcut()
    {
        return pointcut;
    }

    @Override
    public Advice getAdvice()
    {
        return advice;
    }

}
